package com.aiyi.server.manager.nginx.manager;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.aiyi.server.manager.nginx.bean.NginxService;
import com.aiyi.server.manager.nginx.bean.result.Result;

/**
 * Nginx操作类
 *
 * @Project : nginx
 * @Program Name : com.aiyi.server.manager.nginx.manager.NginxManager
 * @Description :
 * @Author : 郭胜凯
 * @Creation Date : 2018年2月1日 下午4:40:28
 * @ModificationHistory Who When What ---------- ------------- -----------------------------------
 * 郭胜凯 2018年2月1日 create
 */
@Component
public class NginxManager {

    private static final Logger logger = LoggerFactory.getLogger(NginxManager.class);

	public NginxService getServiceBy(Integer nginxId) {
		NginxService nginxService = NginxService.getServiceBy(nginxId);
		return nginxService;
	}

	public Result start(Integer nginxId) {
		Result result = new Result();
		NginxService nginxService = NginxService.getServiceBy(nginxId);
		if(nginxService==null) {
			result.setCode("");
			result.setMessage("指定服务("+nginxId+")不存在！");
		} else if(nginxService.isStart()) {
			result.setCode("");
			result.setMessage("指定服务("+nginxId+")原就是启动状态，无需再启动！");
		} else {
			result = nginxService.start();
		}
		return result;
	}

	public Result stop(Integer nginxId) {
		Result result = new Result();
		NginxService nginxService = NginxService.getServiceBy(nginxId);
		if(nginxService==null) {
			result.setCode("");
			result.setMessage("指定服务("+nginxId+")不存在！");
		} else {
			result = nginxService.stop();
		}
		return result;
	}

	public Result reload(Integer nginxId) {
		Result result = new Result();
		NginxService nginxService = NginxService.getServiceBy(nginxId);
		if(nginxService==null) {
			result.setCode("");
			result.setMessage("指定服务("+nginxId+")不存在！");
		} else {
			nginxService.reload();
		}
		return result;
	}

	public List<NginxService> getNginxServiceList() {
		return NginxService.getNginxServerList();
	}
	
}
